home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / t3_1 / risc_src.lha / risc_sources / sys / pool.t < prev    next >
Text File  |  1989-06-30  |  5KB  |  141 lines

  1. (herald pool
  2.   (env tsys))
  3.  
  4. ;;; Copyright (c) 1985 Yale University
  5. ;;;     Authors: N Adams, R Kelsey, D Kranz, J Philbin, J Rees.
  6. ;;; This material was developed by the T Project at the Yale University Computer 
  7. ;;; Science Department.  Permission to copy this software, to redistribute it, 
  8. ;;; and to use it for any purpose is granted, subject to the following restric-
  9. ;;; tions and understandings.
  10. ;;; 1. Any copy made of this software must include this copyright notice in full.
  11. ;;; 2. Users of this software agree to make their best efforts (a) to return
  12. ;;;    to the T Project at Yale any improvements or extensions that they make,
  13. ;;;    so that these may be included in future releases; and (b) to inform
  14. ;;;    the T Project of noteworthy uses of this software.
  15. ;;; 3. All materials developed as a consequence of the use of this software
  16. ;;;    shall duly acknowledge such use, in accordance with the usual standards
  17. ;;;    of acknowledging credit in academic research.
  18. ;;; 4. Yale has made no warrantee or representation that the operation of
  19. ;;;    this software will be error-free, and Yale is under no obligation to
  20. ;;;    provide any services, by way of maintenance, update, or otherwise.
  21. ;;; 5. In conjunction with products arising from the use of this material,
  22. ;;;    there shall be no use of the name of the Yale University nor of any
  23. ;;;    adaptation thereof in any advertising, promotional, or sales literature
  24. ;;;    without prior written consent from Yale in each case.
  25. ;;;
  26.  
  27. ;;; Pools are implemented as simple structures.  They keep statistics
  28. ;;; which are very handy for finding storage leaks.  We should add
  29. ;;; tombstones someday.
  30.  
  31. ;++ In order to do tombstoned pools the compiler probably needs
  32. ;++ to know something about them.  The architecture probably also
  33. ;++ needs to support pointers into objects.
  34. ;++ should pools be using vectors?
  35.  
  36. (define-operation (POOL-STATISTICS POOL))
  37.  
  38. (define-structure-type %pool
  39.   id
  40.   generator
  41.   generate-count
  42.   pred
  43.   weak
  44.   created
  45.   allocated
  46.   freed
  47.   (((print-type-string self) (ignore self) "pool")
  48.    ((identification self) (%pool-id self))
  49.    ((pool-statistics self)
  50.     ;; backquote is not available in the VM.
  51.     (list (list 'pool-statistics (%pool-id self))
  52.           (list 'created         (%pool-created   self))
  53.           (list 'allocated       (%pool-allocated self))
  54.           (list 'freed           (%pool-freed     self))))))
  55.  
  56. ;++ Change when structure package handles defaults
  57. (set (%pool-generate-count  (stype-master %pool-stype)) 1)
  58. (set (%pool-pred            (stype-master %pool-stype)) true)
  59. (set (%pool-created         (stype-master %pool-stype)) 0)
  60. (set (%pool-allocated       (stype-master %pool-stype)) 0)
  61. (set (%pool-freed           (stype-master %pool-stype)) 0)
  62.  
  63. ;++ addition of optional count and predicate argument.  This will
  64. ;++ not be optional in 3.1.
  65.  
  66. (define (make-pool id generator . args)
  67.   (destructure (((count pred) args))
  68.     (if (null? count) (warning "No COUNT argument to MAKE-POOL."))
  69.     (if (null? pred)  (warning "No PREDICATE argument to MAKE-POOL."))
  70.     (%make-pool id generator (if count count 1) (if pred pred true))))
  71.  
  72. (define (%make-pool id generator count pred)
  73.   (let ((pool (make-%pool)))
  74.     (set (%pool-id             pool) id)
  75.     (set (%pool-generator      pool) generator)
  76.     (set (%pool-generate-count pool) count)
  77.     (set (%pool-pred           pool) pred)
  78.     (set (%pool-weak           pool) (make-weak-cell '#f))
  79.     pool))
  80.  
  81. (define pool-freelist
  82.   (make-freelist))
  83.  
  84. ;;; Release the storage associated with an object.
  85. (define-operation (recycle obj) (return))
  86.  
  87. (define (obtain-from-pool pool)
  88.   (set (%pool-allocated pool) (fx+ 1 (%pool-allocated pool)))
  89.   (let* ((weak (%pool-weak pool))
  90.          (elts (weak-cell-contents weak)))
  91.     (receive (obj elts)
  92.              (if (not elts)
  93.                  (return ((%pool-generator pool)) (cons-a-pool-freelist pool))
  94.                  (let ((a (car elts))
  95.                        (d (cdr elts)))
  96.                    (return-to-this-freelist pool-freelist elts)
  97.                    (return a d)))
  98.       (set (weak-cell-contents weak) elts)
  99.       obj)))
  100.  
  101. (define (cons-a-pool-freelist pool)
  102.   (let ((generator (%pool-generator pool))
  103.         (count (%pool-generate-count pool)))
  104.     (do ((i 1 (fx+ i 1))
  105.          (l '() (cons-from-this-freelist pool-freelist (generator) l)))
  106.         ((fx>= i count) 
  107.          (set (%pool-created pool) (fx+ i (%pool-created pool)))
  108.          l))))
  109.  
  110. (lset zeroed-storage 0)
  111.  
  112. (define (return-to-pool pool obj)
  113.   (set (%pool-freed pool) (fx+ 1 (%pool-freed pool)))
  114.   (cond ((points-to-initial-impure-memory? obj)
  115.          (if (extend? obj) (zero-out-storage obj))
  116.          (return))
  117.         (else
  118.          (let* ((weak (%pool-weak pool))
  119.                 (obj  (enforce (%pool-pred pool) obj)))
  120.            (modify (weak-cell-contents weak)
  121.                    (lambda (l)
  122.                      (cons-from-this-freelist pool-freelist obj l)))
  123.            (return)))))
  124.  
  125. (define (zero-out-storage obj)
  126.   (let ((count (extend-pointers obj)))
  127.     (modify zeroed-storage (lambda (x) (fx+ x count)))
  128.     (do ((i (fx- count 1) (fx- i 1)))
  129.         ((fx< i 0))
  130.       (set (extend-elt obj i) 0))))
  131.  
  132. (define (extend-pointers ptr)
  133.   (let ((header (extend-header ptr)))
  134.     (receive (ptr #f #f)
  135.              (cond ((template? header)
  136.                     (scan-closure ptr header))
  137.                    (else
  138.                     ((vref *scan-dispatch-vector* (header-type header)) ptr)))
  139.       ptr)))
  140.  
  141.